package com.norma.netty.study.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author zhangchaofeng
 * @date 2020/10/5
 * @description
 */
public class NettyMessageDecoder extends ReplayingDecoder<NettyMessageProtocol> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("---------> NettyMessageDecoder()");
        //需要将二进制字节码转成NettyMessageProtocol数据包对象
        int len = in.readInt();
        byte[] content = new byte[len];
        in.readBytes(content);

        NettyMessageProtocol message = new NettyMessageProtocol();
        message.setContent(content);
        message.setLength(len);
        out.add(message);

    }
}
